package com.skplanet.oneplusonewindow.moimserver.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.skplanet.oneplusonewindow.moimserver.dao.MoimDAO;
import com.skplanet.oneplusonewindow.moimserver.dao.PlazaBoardDAO;
import com.skplanet.oneplusonewindow.moimserver.dao.PlazaCommentDAO;
import com.skplanet.oneplusonewindow.moimserver.dao.PlazaDAO;
import com.skplanet.oneplusonewindow.moimserver.dao.UserDAO;
import com.skplanet.oneplusonewindow.moimserver.domain.Moim_Moim;
import com.skplanet.oneplusonewindow.moimserver.domain.Moim_Plaza;
import com.skplanet.oneplusonewindow.moimserver.domain.Moim_PlazaBoard;
import com.skplanet.oneplusonewindow.moimserver.domain.Moim_PlazaComment;



@Service(value="plazaService")
@Transactional
public class PlazaService {
	@Autowired
	private UserDAO userDAO;
	@Autowired
	private PlazaDAO plazaDAO;
	@Autowired
	private MoimDAO moimDAO;
	@Autowired
	private PlazaBoardDAO plazaboardDAO;
	@Autowired
	private PlazaCommentDAO commentDAO;
	
	@Transactional(readOnly = true)
	public Moim_Plaza getPlaza(long id){
		Moim_Plaza plaza = plazaDAO.findUniqByObject("id", id);
		return plaza;
	}
	@Transactional(readOnly = true)
	public Moim_PlazaBoard getPlazaBoard(long id){
		Moim_PlazaBoard plazaboard = plazaboardDAO.findUniqByObject("id", id);
		return plazaboard;
	}
	@Transactional(readOnly = true)
	public Moim_PlazaComment getComment(long id){
		Moim_PlazaComment comment = commentDAO.findUniqByObject("id", id);
		return comment;
	}
	
	@Transactional
	public void updateReadCount(long id){
		Moim_PlazaBoard plazaboard = plazaboardDAO.findUniqByObject("id", id);
		plazaboard.setReadcount(plazaboard.getReadcount() + 1);
	}
	
	@Transactional(readOnly = true)
	public int getBoardCount(long plazaId){
		Moim_Plaza plaza = plazaDAO.findUniqByObject("id", plazaId);
		return plaza.getPlazaboards().size();
	}
	@Transactional(readOnly = true)
	public int getPlazaCount(long moimId){
		Moim_Moim moim = moimDAO.findUniqByObject("id", moimId);
		return moim.getPlazas().size();
	}
	
	@Transactional(readOnly = true)
	public List<Moim_PlazaBoard> getBoardList(long plazaId){
		Moim_Plaza plaza = plazaDAO.findUniqByObject("id", plazaId);
		return plaza.getPlazaboards();
	}
	@Transactional(readOnly = true)
	public List<Moim_Plaza> getPlazaList(long moimId){
		Moim_Moim moim = moimDAO.findUniqByObject("id", moimId);
		return moim.getPlazas();
	}
	@Transactional(readOnly = true)
	public List<Moim_PlazaComment> getCommentList(long id){
		Moim_PlazaBoard board = plazaboardDAO.findUniqByObject("id", id);
		return board.getComments();
	}
	
	@Transactional
	public void addBoard(long plazaId, Moim_PlazaBoard board){
		plazaboardDAO.save(board);
		Moim_PlazaBoard board1 = plazaboardDAO.findUniqByObject("id", board.getId());
		Moim_Plaza plaza = plazaDAO.findUniqByObject("id", plazaId);
		board1.setPlaza(plaza);
		plaza.addPlazaboard(board1);
	}
	@Transactional
	public void addPlaza(long moimId, Moim_Plaza plaza){
		plazaDAO.save(plaza);
		Moim_Plaza plaza1 = plazaDAO.findUniqByObject("id", plaza.getId());
		Moim_Moim moim = moimDAO.findUniqByObject("id", moimId);
		plaza1.setMoim2(moim);
		moim.addPlaza(plaza1);
	}
	
	@Transactional
	public void updateBoard(long id, Moim_PlazaBoard board){
		Moim_PlazaBoard b1 = plazaboardDAO.findUniqByObject("id", id);
		b1.setContent(board.getContent());
		b1.setTitle(board.getTitle());
	}
	@Transactional
	public void updatePlaza(long id, Moim_Plaza plaza){
		Moim_Plaza p1 = plazaDAO.findUniqByObject("id", id);
		p1.setContent(plaza.getContent());
		p1.setTitle(plaza.getTitle());
	}
	
	@Transactional
	public void updateComment(long id, Moim_PlazaComment comment){
		Moim_PlazaComment c1 = commentDAO.findUniqByObject("id", id);
		c1.setContent(comment.getContent());
	}
	@Transactional
	public void deleteComment(Moim_PlazaComment comment){
		commentDAO.delete(comment);
	}	

	@Transactional
	public void deleteBoard(Moim_PlazaBoard plazaboard){
		plazaboardDAO.delete(plazaboard);
	}
	@Transactional
	public void deletePlaza(Moim_Plaza plaza){
		plazaDAO.delete(plaza);
	}
	
	@Transactional
	public void addComment(long plazaboardid, Moim_PlazaComment comment){
		commentDAO.save(comment);
		Moim_PlazaComment comment1 = commentDAO.findUniqByObject("id", comment.getId());
		Moim_PlazaBoard board = getPlazaBoard(plazaboardid);
		comment1.setPlazaboard(board);
		board.addComment(comment1);
	}
}
